package com.example.demo.service.imply;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.annotation.AutoFill;
import com.example.demo.enumeration.OperationType;
import com.example.demo.mapper.ComponentMapper;
import com.example.demo.mapper.InboundMapper;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.dto.InboundDTO;
import com.example.demo.pojo.dto.InventoryDTO;
import com.example.demo.pojo.entity.Component;
import com.example.demo.pojo.entity.Inbound;
import com.example.demo.pojo.entity.Inventory;
import com.example.demo.pojo.entity.Storage;
import com.example.demo.pojo.query.InboundQuery;
import com.example.demo.pojo.vo.InboundVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.IInboundService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IInboundServiceImply extends ServiceImpl<InboundMapper, Inbound> implements IInboundService {
    private final IInventoryServiceImply inventoryService;
    private final ComponentMapper componentMapper;
    private final StorageMapper storageMapper;
    private final InventoryMapper inventoryMapper;
    @Override
    @AutoFill(OperationType.INSERT)
    public int addStorage(InboundDTO inboundDTO, PublicFields publicFields) {
        Inbound inbound = new Inbound();
        BeanUtils.copyProperties(inboundDTO, inbound);
        BeanUtils.copyProperties(publicFields, inbound);
        int addNumber=inbound.getInQuantity();
        int old_number=0;
        Inventory inventory=inventoryMapper.selectByIds(inbound.getStorageId(),inbound.getComponentId());
        if(inventory==null){
            Inventory addInventory=new Inventory();
            addInventory.setStorageId(inbound.getStorageId());
            addInventory.setComponentId(inbound.getComponentId());
            addInventory.setNumber(inbound.getInQuantity());
            addInventory.setUpdateTime(LocalDateTime.now());
            inventoryMapper.insert(addInventory);
        }else{
            old_number=inventory.getNumber();
        }
        int new_number=old_number+addNumber;
        InventoryDTO inventoryDTO=new InventoryDTO();
        inventoryDTO.setNumber(new_number);
        inventoryDTO.setStorageId(inbound.getStorageId());
        inventoryDTO.setComponentId(inbound.getComponentId());
        //更新数量
        inventoryService.updateInventory(inventoryDTO,PublicFields.newInstance());
        //更新价钱
        double price=componentMapper.selectById(inboundDTO.getComponentId()).getPrice()*inboundDTO.getInQuantity();
        inbound.setPrice(price);
        baseMapper.insert(inbound);
        return  1;
    }

    @Override
    public void deleteInbound(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageVO<InboundVO> queryComponentPage(InboundQuery inboundQuery) {
        Page<Inbound> p = Page.of(inboundQuery.getPageNo(), inboundQuery.getPageSize());
        p.addOrder(OrderItem.desc("update_time"));
        PageVO<InboundVO> inboundVOs = new PageVO<>();
        Page<Inbound> page = lambdaQuery()
                .like(StringUtils.hasText(inboundQuery.getStorageId()), Inbound::getStorageId, inboundQuery.getStorageId())
                .like(StringUtils.hasText(inboundQuery.getComponentId()), Inbound::getComponentId, inboundQuery.getComponentId())
                .page(p);
        inboundVOs.setPages(p.getPages());
        inboundVOs.setTotal(p.getTotal());
        List<Inbound> records = p.getRecords();
        List<InboundVO> InboundVOList = records.stream().map(inbound -> {
            InboundVO inboundVO = new InboundVO();
            BeanUtils.copyProperties(inbound, inboundVO);
            Component component=componentMapper.selectById(inbound.getComponentId());
            Storage storage=storageMapper.selectById(inbound.getStorageId());
            inboundVO.setComponentName(component.getComponentName());
            inboundVO.setStorageName(storage.getStorageName());
            return inboundVO;
        }).collect(Collectors.toList());
        inboundVOs.setList(InboundVOList);
        return inboundVOs;
    }


}
